using System;
using System.Collections.Generic;
using Rilisoft.MiniJson;
using UnityEngine;

[Serializable]
public class GotchaChest
{
	public GotchaChestType type;

	public MoneyType openMoney;

	public int maxOpenPrice;

	[HideInInspector]
	public int actualOpenPrice;

	public float costModifier = 5f;

	public int secondsToOpen = 86400;

	public List<GotchaRewardGroup> rewardGroups;

	public int count
	{
		get
		{
			return BCWStorage.Get<int>(string.Concat(type, "_count"));
		}
		set
		{
			if (value < 0)
			{
				value = 0;
			}
			if (type == GotchaChestType.Wooden && value < 1)
			{
				value = 1;
			}
			BCWStorage.Set(string.Concat(type, "_count"), value, true);
			if (this.countChanged != null)
			{
				this.countChanged();
			}
		}
	}

	public int unlockTime
	{
		get
		{
			return BCWStorage.Get<int>(string.Concat(type, "_openTime"));
		}
		set
		{
			BCWStorage.Set(string.Concat(type, "_openTime"), value, true);
			if (this.unlockTimeChanged != null)
			{
				this.unlockTimeChanged();
			}
		}
	}

	public float moneyBox
	{
		get
		{
			return BCWStorage.Get<float>(string.Concat(type, "_moneyBox"));
		}
		set
		{
			if (value < 0f)
			{
				value = 0f;
			}
			BCWStorage.Set(string.Concat(type, "_moneyBox"), value, true);
		}
	}

	private bool addedWithNoTime
	{
		get
		{
			return BCWStorage.Get<bool>(string.Concat(type, "_addedWithNoTime"));
		}
		set
		{
			BCWStorage.Set(string.Concat(type, "_addedWithNoTime"), value);
		}
	}

	public event Action countChanged;

	public event Action unlockTimeChanged;

	public bool IsUnlocked()
	{
		return count > 0 && !addedWithNoTime && unlockTime != 0 && unlockTime <= JSONManager.Instance.GetServerTime();
	}

	public bool Unlock()
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		if (IsUnlocked())
		{
			dictionary.Add(type.ToString(), "Free_open");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_CHESTS_OPEN_TYPE, dictionary);
			return true;
		}
		if (count > 0)
		{
			if (openMoney == MoneyType.Cash && settings.CathedExeptions >= actualOpenPrice)
			{
				settings.CathedExeptions -= actualOpenPrice;
				unlockTime = JSONManager.Instance.GetServerTime() - 1;
				dictionary.Add(type.ToString(), "Hard_open");
				FlurryWrapper.LogEvent(FlurryWrapper.EV_CHESTS_OPEN_TYPE, dictionary);
				return true;
			}
			if (openMoney == MoneyType.Gold && settings.CathedWarnings >= actualOpenPrice)
			{
				settings.CathedWarnings -= actualOpenPrice;
				unlockTime = JSONManager.Instance.GetServerTime() - 1;
				dictionary.Add(type.ToString(), "Hard_open");
				FlurryWrapper.LogEvent(FlurryWrapper.EV_CHESTS_OPEN_TYPE, dictionary);
				return true;
			}
			CoroutineRunner.Instance.StartCoroutine(MarketControllerUI.instance.ShowBank(openMoney));
		}
		return false;
	}

	public void Open()
	{
		count--;
		if (count > 0)
		{
			UpdateUnlockTime();
		}
	}

	public void Add(int count)
	{
		if (this.count == 0 && count > 0)
		{
			UpdateUnlockTime();
		}
		this.count += count;
	}

	private void UpdateUnlockTime()
	{
		if (JSONManager.Instance.ServerTimeLoad())
		{
			addedWithNoTime = false;
			unlockTime = JSONManager.Instance.GetServerTime() + secondsToOpen;
		}
		else
		{
			addedWithNoTime = true;
		}
	}

	public void SetActualOpenPrice()
	{
		actualOpenPrice = maxOpenPrice;
		if (JSONManager.Instance.ServerTimeLoad())
		{
			actualOpenPrice = (int)((float)maxOpenPrice * ((float)(unlockTime - JSONManager.Instance.GetServerTime()) / (float)secondsToOpen));
			if (actualOpenPrice <= 0)
			{
				actualOpenPrice = 1;
			}
		}
	}

	public void ResetTimer()
	{
		if (addedWithNoTime)
		{
			UpdateUnlockTime();
		}
	}

	public List<GotchaReward> GetChestRewards()
	{
		List<GotchaReward> list = TryLoadActualRewards();
		if (list == null || list.Count == 0)
		{
			list = new List<GotchaReward>();
			foreach (GotchaRewardGroup rewardGroup in rewardGroups)
			{
				GotchaReward rewardFromGroup = GetRewardFromGroup(rewardGroup);
				if (rewardFromGroup != null)
				{
					list.Add(rewardFromGroup);
				}
			}
			SaveActualRewards(list);
			return list;
		}
		return GetActualRewards(list);
	}

	public GotchaReward GetRewardFromGroup(GotchaRewardGroup rewardGroup)
	{
		GotchaReward result = null;
		List<GotchaReward> actualRewards = GetActualRewards(rewardGroup.rewards);
		if (actualRewards.Count > 0)
		{
			switch (rewardGroup.selectionType)
			{
			case GotchaRewardSelectionType.Random:
				result = actualRewards[UnityEngine.Random.Range(0, actualRewards.Count)];
				break;
			case GotchaRewardSelectionType.Order:
				result = actualRewards[0];
				break;
			}
		}
		return result;
	}

	public List<GotchaReward> GetActualRewards(List<GotchaReward> rewardList)
	{
		List<GotchaReward> list = new List<GotchaReward>();
		foreach (GotchaReward reward in rewardList)
		{
			if (reward.type == GotchaRewardType.Product)
			{
				Product product = MarketController.Instance.GetProduct(reward.id);
				if ((product.prevProduct == null || product.prevProduct.Purchased) && !product.Purchased)
				{
					list.Add(reward);
				}
			}
			else
			{
				list.Add(reward);
			}
		}
		return list;
	}

	public string CreateActualRewardsJson(List<GotchaReward> rewards)
	{
		Dictionary<string, object> dictionary = new Dictionary<string, object>();
		for (int i = 0; i < rewards.Count; i++)
		{
			Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
			dictionary2.Add("id", rewards[i].id);
			dictionary2.Add("type", (int)rewards[i].type);
			dictionary2.Add("amount", rewards[i].amount);
			dictionary2.Add("cost", rewards[i].cost);
			dictionary.Add("GotchaReward_" + i, dictionary2);
		}
		return Json.Serialize(dictionary);
	}

	public void SaveActualRewards(List<GotchaReward> rewards)
	{
		string key = "Actual_rewards_" + type;
		string value = CreateActualRewardsJson(rewards);
		BCWStorage.Set(key, value);
	}

	public List<GotchaReward> TryLoadActualRewards()
	{
		string key = "Actual_rewards_" + type;
		string json = BCWStorage.Get<string>(key);
		Dictionary<string, object> dictionary = Json.Deserialize(json) as Dictionary<string, object>;
		if (dictionary == null)
		{
			return null;
		}
		List<GotchaReward> list = new List<GotchaReward>();
		for (int i = 0; i < dictionary.Count; i++)
		{
			Dictionary<string, object> dictionary2 = (Dictionary<string, object>)dictionary["GotchaReward_" + i];
			GotchaReward gotchaReward = new GotchaReward();
			object value;
			dictionary2.TryGetValue("id", out value);
			gotchaReward.id = value.ToString();
			object value2;
			dictionary2.TryGetValue("type", out value2);
			gotchaReward.type = (GotchaRewardType)Convert.ToInt32(value2);
			object value3;
			dictionary2.TryGetValue("amount", out value3);
			gotchaReward.amount = Convert.ToInt32(value3);
			object value4;
			dictionary2.TryGetValue("cost", out value4);
			gotchaReward.cost = Convert.ToSingle(value4);
			list.Add(gotchaReward);
		}
		return list;
	}

	public void ClearActualRewards()
	{
		string key = "Actual_rewards_" + type;
		BCWStorage.Set(key, string.Empty);
	}
}
